package com.wthink.hzgj.service.jt808

import com.wthink.hzgj.data.domain.CheckinType
import com.wthink.hzgj.util.BCD
import com.wthink.hzgj.util.PreferencesUtils
import java.math.BigDecimal
import java.nio.ByteBuffer
import java.text.SimpleDateFormat
import java.util.*

/**
 * 考勤请求
 */
class JT_0B05(
    var line: Int, var user: String, var checkinType: CheckinType
) : JT808Message() {

    override fun writeToBytes(): ByteArray {
        val buf = ByteBuffer.allocate(13 + user.length)
        buf.putInt(line)
        buf.put(user.toByteArray())
        buf.put(0)
        val mix = PreferencesUtils.getLong("mix")
        val nowTime : BigDecimal = BigDecimal(mix).add(BigDecimal(System.currentTimeMillis()))
        val time = SimpleDateFormat("yyMMddHHmmss").format(nowTime)
        buf.put(BCD.ASCII_To_BCD(time.toByteArray(), time.length))
        when (checkinType) {
            CheckinType.SB -> buf.put(1)
            CheckinType.XB -> buf.put(2)
            CheckinType.QD -> buf.put(3)
        }
        buf.put(1)

        buf.flip()
        val body = ByteArray(buf.remaining())
        buf.get(body)
        return body
    }

}